﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;

namespace BusinessLogic
{
    public static class TyLeTinh
    {

        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.TyLeTinh[] Select()
        {
            Entities.TyLeTinh[] list = null;
            try
            {
                string sql = Common.Constants.TyLeTinh.Select;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.TyLeTinh TyLeTinh = new Entities.TyLeTinh();
                    TyLeTinh.TyLeTinhID = int.Parse(dr[Common.Constants.TyLeTinh.TyLeTinhID].ToString());
                    TyLeTinh.MaTyLeTinh = dr[Common.Constants.TyLeTinh.MaTyLeTinh].ToString();
                    TyLeTinh.SoTien = dr[Common.Constants.TyLeTinh.SoTien].ToString();
                    TyLeTinh.NgayNhap = DateTime.Parse(dr[Common.Constants.TyLeTinh.NgayNhap].ToString());
                    TyLeTinh.GhiChu = dr[Common.Constants.TyLeTinh.GhiChu].ToString();
                    TyLeTinh.Deleted = bool.Parse(dr[Common.Constants.TyLeTinh.Deleted].ToString());
                    arr.Add(TyLeTinh);
                }
                return (Entities.TyLeTinh[])arr.ToArray(typeof(Entities.TyLeTinh));
            }
            catch
            { return new Entities.TyLeTinh[0]; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }

        }
        #endregion

        #region insert
        public static int InsertUpdate(Entities.TyLeTinh TyLeTinh)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.TyLeTinh.InsertUpdate;
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.TyLeTinh.HanhDong, SqlDbType.NVarChar, 50).Value = TyLeTinh.HanhDong;
                    cmd.Parameters.Add(Common.Constants.TyLeTinh.TyLeTinhID, SqlDbType.Int).Value = TyLeTinh.TyLeTinhID;
                    cmd.Parameters.Add(Common.Constants.TyLeTinh.MaTyLeTinh, SqlDbType.VarChar, 50).Value = TyLeTinh.MaTyLeTinh;
                    cmd.Parameters.Add(Common.Constants.TyLeTinh.SoTien, SqlDbType.VarChar, 50).Value = TyLeTinh.SoTien;
                    cmd.Parameters.Add(Common.Constants.TyLeTinh.NgayNhap, SqlDbType.DateTime).Value = TyLeTinh.NgayNhap;
                    cmd.Parameters.Add(Common.Constants.TyLeTinh.GhiChu, SqlDbType.NVarChar, 200).Value = TyLeTinh.GhiChu;
                    cmd.Parameters.Add(Common.Constants.TyLeTinh.Deleted, SqlDbType.Bit).Value = TyLeTinh.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    LuuVietHeThong.LuuViet(
                        "Thêm tỷ lệ tính",
                        TyLeTinh.TaiKhoan.MaNhanVien,
                        "Thêm tỷ lệ tính mới " + TyLeTinh.MaTyLeTinh,
                        TyLeTinh.TaiKhoan.TenDangNhap,
                        DateTime.Now.ToString("dd/MM/YYYY"));
                    ts.Complete();
                    return rt;
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                return 0;
            }

        }
        #endregion
    }
}
#endregion